#include "TCPSocket.h"
TCPSocket* TCPSocket::tcp_socket=nullptr;
TCPSocket::TCPSocket()
{
    timer = new QTimer();

    connect(timer, &QTimer::timeout, this, &TCPSocket::startTimer);


}
TCPSocket::~TCPSocket()
{
    //TODO send close request

}
void TCPSocket::TCPconnect(){


    this->connectToHost("101.34.151.88",6677);

    timer->start(2000);
    qDebug()<<"TCP socket connect start";

}
void TCPSocket::startTimer()
{
    if(state()==UnconnectedState)
    {
        this->connectToHost("101.34.151.88",6677);
        qDebug()<<"socket error ,try restart";
    }


}
void TCPSocket::myWrite(const QByteArray& data)
{
    qDebug()<<"TCPSocket thread id "<<QThread::currentThreadId();
    if(this->write(data)==-1)
    {
        qDebug()<<"write error";
    }
    qDebug()<<"size: "<<data.size();
    qDebug()<<"TCPSocket "<<data;
}
void TCPSocket::myRead(QByteArray &data)
{
    data = this->readAll();
}
